MAKEFLAGS += --no-print-directory

#
# Setup Makefile to match your environment
#
PYTHON=/usr/bin/python3

# check for executables

BLACK = $(shell which black)
HTTPD = $(shell which httpd)
APACHE2 = $(shell which apache2)

# Debian / Ubuntu have /bin/sh -> dash
SHELL = /bin/bash

NAME=$(shell basename $(CURDIR))
VERSION=$(shell grep -Po '(?<=VERSION = ")[0-9]+\.[0-9]+\.[0-9]+(?=")' setup.py)
TOP_DIR:=$(shell pwd)
DESTDIR=/
SYSTESTS="basic_test or import_test or settings_cli_test or svc_test"

prefix=devinstall

# Taken from: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

all: clean build ## Executes the clean target and afterwards the build target.

clean: ## Cleans Python bytecode, build artifacts and the temp files.
	@echo "cleaning: python bytecode"
	@rm -f *.pyc
	@rm -f cobbler/*.pyc
	@rm -f cobbler/modules/*.pyc
	@rm -f cobbler/web/*.pyc
	@rm -f cobbler/web/templatetags/*.pyc
	@rm -rf cobbler/__pycache__
	@rm -rf cobbler/**/__pycache__
	@echo "cleaning: build artifacts"
	@rm -rf build release dist cobbler.egg-info
	@rm -rf rpm-build/*
	@rm -rf deb-build/*
	@rm -f MANIFEST AUTHORS
	@rm -f config/version
	@rm -f docs/*.1.gz
	@rm -rf docs/_build
	@echo "cleaning: temp files"
	@rm -f *~
	@rm -rf buildiso
	@rm -f *.tmp
	@rm -f *.log
	@rm -f supervisord.pid
	@rm -rf .pytest_cache

cleandoc: ## Cleans the docs directory.
	@echo "cleaning: documentation"
	@cd docs; make clean > /dev/null 2>&1

doc: ## Creates the documentation with sphinx in html form.
	@echo "creating: documentation"
	@cd docs; make html > /dev/null 2>&1

man: ## Creates documentation and man pages using Sphinx
	@cd docs; make man > /dev/null 2>&1

qa: ## If black is found then it is run.
ifeq ($(strip $(BLACK)),)
	@echo "No black found"
else
	@echo "checking: black ${BLACK}"
	@${BLACK} .
endif

authors: ## Creates the AUTHORS file.
	@echo "creating: AUTHORS"
	@cp AUTHORS.in AUTHORS
	@git config --global --add safe.directory /code
	@git log --format='%aN <%aE>' | grep -v 'root' | sort -u >> AUTHORS

release: clean qa authors ## Creates the full release.
	@echo "creating: release artifacts"
	@bash -c 'name=${NAME}; cd ..; tar --transform="s/${NAME}/${NAME}-${VERSION}/" --exclude ".idea" --exclude ".mypy_cache" --exclude="venv" --exclude "dist" --exclude "build" -czf "$$name.tar.gz" "$$name"'
	@mkdir release
	@mv ../${NAME}.tar.gz ./release/${NAME}-${VERSION}.tar.gz

test-rocky9: ## Executes the testscript for testing cobbler in a docker container on Rocky Linux 9.
	./docker/rpms/build-and-install-rpms.sh rl8 docker/rpms/Rocky_Linux_9/Rocky_Linux_9.dockerfile

test-rocky10: ## Executes the testscript for testing cobbler in a docker container on Rocky Linux 10.
	./docker/rpms/build-and-install-rpms.sh rl10 docker/rpms/Rocky_Linux_10/Rocky_Linux_10.dockerfile

test-fedora41: ## Executes the testscript for testing cobbler in a docker container on Fedora 41.
	./docker/rpms/build-and-install-rpms.sh fc41 docker/rpms/Fedora_41/Fedora41.dockerfile

test-debian11: ## Executes the testscript for testing cobbler in a docker container on Debian 11.
	./docker/debs/build-and-install-debs.sh deb11 docker/debs/Debian_11/Debian11.dockerfile

test-debian12: ## Executes the testscript for testing cobbler in a docker container on Debian 12.
	./docker/debs/build-and-install-debs.sh deb12 docker/debs/Debian_12/Debian12.dockerfile

system-test: ## Runs the system tests
	${PYTHON} -m pytest -m integration -k ${SYSTESTS}

build: authors ## Runs the Python Build.
	@echo "building: manpages"
	@cd docs; make man > /dev/null 2>&1
	@echo "building: Python package"
	@git config --global --add safe.directory /code; \
	${PYTHON} -m pip wheel --verbose --wheel-dir ./build .

install: build ## Runs the build target and then installs via setup.py
	@git config --global --add safe.directory /code; \
	${PYTHON} -m pip install --verbose --no-compile --no-deps --no-index --root $(DESTDIR) ./build/cobbler*.whl

rpms: release ## Runs the target release and then creates via rpmbuild the rpms in a directory called rpm-build.
	mkdir -p rpm-build/SOURCES
	cp release/*.gz rpm-build/SOURCES
	rpmbuild --define "_topdir %(pwd)/rpm-build" \
	--define '_rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' \
	-ba cobbler.spec

# Only build a binary package
debs: authors ## Creates native debs in a directory called deb-build. The release target is called during the build process.
	@debuild -us -uc
	@mkdir -p deb-build; \
    cp ../cobbler_* deb-build/; \
    cp ../cobbler-tests* deb-build/

eraseconfig: ## Deletes the cobbler data jsons which are created when using the file provider.
	-rm /var/lib/cobbler/cobbler_collections/distros/*
	-rm /var/lib/cobbler/cobbler_collections/images/*
	-rm /var/lib/cobbler/cobbler_collections/profiles/*
	-rm /var/lib/cobbler/cobbler_collections/systems/*
	-rm /var/lib/cobbler/cobbler_collections/repos/*
	-rm /var/lib/cobbler/cobbler_collections/mgmtclasses/*
	-rm /var/lib/cobbler/cobbler_collections/files/*
	-rm /var/lib/cobbler/cobbler_collections/packages/*
	-rm /var/lib/cobbler/cobbler_collections/menus/*

.PHONY: system-test
